
#include "data_split_group.h"
/*
数据拆分
为了避免误判包的起始与结束，数据包和校验码里的0xFF，0xFE和0xFD
destin-- 指向拆分后的数据源
source-- 指向拆分前的数据源
source_len    -- 指向拆分前的数据源的长度
返回拆分 后的数据长度
*/
 unsigned char data_split(void *destin, void *source, char source_len)
{
	unsigned char len = 0;
	while (source_len--)
	{
		if (*(unsigned char *)source >= 0xfd)
		{
			*(unsigned char *)destin = 0xfd;
			destin = (unsigned char *)destin + 1;
			*(unsigned char *)destin = *(unsigned char *)source - 0xfd;
			len++;
		}
		else
		{
			*(unsigned char *)destin = *(unsigned char *)source;
		}
		destin = (unsigned char *)destin + 1;
		source = (unsigned char *)source + 1;
		len++;
	}
	return len;
}
/*
数据组合
为了避免误判包的起始与结束，数据包和校验码里的0xFF，0xFE和0xFD
destin-- 指向组合后的数据源
source-- 指向组合前的数据源
return    组合后的数据源长度
*/
 unsigned char data_group(void *destin, void *source, char source_len)
{
	unsigned char len = 0;
	while (source_len--)
	{
		*(unsigned char *)destin = *(unsigned char *)source;
		if (*(unsigned char *)source == 0xfd)
		{
			source = (unsigned char *)source + 1;
			*(unsigned char *)destin += *(unsigned char *)source;
			if (source_len != 0)
			{
				source_len--;
			}
		}
		destin = (unsigned char *)destin + 1;
		source = (unsigned char *)source + 1;
		len++;
	}
	return len;
}
